<?php

/**
 * manage blog with comment
 * @author Tuong Tran <tuong.tran@outlook.com>
 */
class StreetController extends Controller {

    private $_model;
    public $activeMenu = 'street';

    public function actionIndex() {
        $models = new Street();

        $models->unsetAttributes();  // clear any default values
        if (isset($_GET['Street']))
            $models->attributes = $_GET['Street'];

        $this->render('index', array(
            'models' => $models,
        ));
    }

    public function actionCreate() {
        $model = new Street();

        if (isset($_POST['Street'])) {
            $model->attributes = $_POST['Street'];

            if ($model->save()) {
                Yii::app()->user->setFlash('success', 'Street successfully added');
                $this->redirect('index');
            }
        }

        $this->render('create', compact('model'));
    }

    public function actionUpdate() {
        $model = $this->loadModel();

        if (isset($_POST['Street'])) {
            $model->attributes = $_POST['Street'];

            if ($model->save()) {
                Yii::app()->user->setFlash('success', 'Street successfully updated');
                $this->redirect('index');
            }
        }

        $this->render('create', compact('model'));
    }

    public function actionDelete() {

        if (Yii::app()->request->isPostRequest) {
            // we only allow deletion via POST request

            $this->loadModel()->delete();

            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

            if (!isset($_GET['ajax']))
                $this->redirect(array('index'));
        }

        else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }

    public function loadModel() {

        if ($this->_model === null) {

            if (isset($_GET['id'])) {

                $this->_model = Street::model()->findByPk($_GET['id']);
            }

            if ($this->_model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
        }

        return $this->_model;
    }

}